package com.gome.han.beencloud.jdk.bio.echo.demo1;
/**
* @author hanpeng
* @version 创建时间：2020年10月27日 下午8:39:57
* @ClassName 类名称
* @Description 类描述
* 总体概述 ServerSocket 负责绑定ip 启动端口监听 
* Socket 负责发起连接 读写数据
* 
*/

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.gome.han.beencloud.Const;
public class BioServer {
	private static ServerSocket  serverSocket;
	private static ExecutorService executorService=Executors.newFixedThreadPool(5);
	
	@SuppressWarnings("resource")
	private static void start() throws IOException {
		try {
			serverSocket = new ServerSocket(Const.DEFAULT_PORT);
			System.out.println("服务器已启动,端口号: "+Const.DEFAULT_PORT);
			while (true) {
				Socket socket = serverSocket.accept();
				System.out.println("有新的客户端连接");
				executorService.execute(new BioServerHandler(socket));
			}
		} finally {
			if(serverSocket!=null) {
				serverSocket.close();
			}
		}
	}
	public static void main(String[] args) throws IOException {
		start();
		/*@SuppressWarnings("resource")
		ServerSocket serverSocket2 = new ServerSocket(0);
		System.out.println(serverSocket2.getLocalPort());
		serverSocket2.close();*/
	}
	
}
